<template>
  <div :id="id" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'
export default {
  name: 'BarMonthmonthSuggest',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart2'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '500px'
    },
    dateData: {
      type: String,
      default: ''
    },
    monthSuggest: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    monthSuggest: {
      handler(newValue, oldValue) {
        this.initChart()
      },
      deep: true
    }
  },
  mounted() {
    if (typeof this.monthSuggest !== 'undefined' && this.monthSuggest != null && this.monthSuggest.length > 0) {
      this.initChart()
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      const xAxisData = []
      const suggest1 = []
      const suggest2 = []
      const suggest3 = []
      // const month = this.monthSuggest[0].statDate.substr(5, 2)
      this.monthSuggest.forEach((item) => {
        const d = item.statDate.split('/')
        xAxisData.push(d[2])
        suggest1.push(item.suggestResult1)
        suggest2.push(item.suggestResult2)
        suggest3.push(item.suggestResult3)
      })
      this.chart = echarts.init(document.getElementById(this.id))
      this.chart.setOption({
        title: {
          text: this.dateData + '月决策建议分布',
          left: 'center'
        },
        legend: {
          orient: 'vertical',
          left: '90%',
          padding: [0, 0, -10, 0]
          // top: '-8px'
        },
        grid: {
          left: '5%',
          right: '7%',
          bottom: '0%',
          top: '15%',
          containLabel: true
        },
        tooltip: {
          formatter: function(p) {
            var result = '<div>' + '<span style="display:inline-block;margin-right:5px;margin-bottom:0;border-radius:10px;width:9px;height:9px;background-color: ' + p.color + ';"></span>' + p.seriesName + '</div>' + '日期：'
            result += p.name + '<br>' + '数量：' + p.value
            return result
          }
        },
        xAxis: {
          axisTick: {
            alignWithLabel: true
          },
          // name: '单位：号',
          nameTextStyle: {
            color: '#4682b4',
            fontSize: 15
          },
          type: 'category',
          data: xAxisData
        },
        yAxis: {
          type: 'value',
          name: '数量（份）',
          nameTextStyle: {
            color: '#4682b4',
            fontSize: 15,
            padding: 15
          }
          // axisLabel: {
          //   formatter: '{value} 份'
          // }
        },
        series: [
          {
            type: 'bar',
            name: '建议拒绝',
            data: suggest2,
            itemStyle: {
              normal: {
                color: '#f84343'
              }
            }
          },
          {
            type: 'bar',
            name: '建议人工审核',
            data: suggest3,
            itemStyle: {
              normal: {
                color: '#ff9900'
              }
            }
          },
          {
            type: 'bar',
            name: '风险较低',
            data: suggest1,
            itemStyle: {
              normal: {
                color: '#00b9ff'
              }
            }
          }
        ]
      })
    }
  }

}
</script>

<style scoped>

</style>
